package com.tjl.yangxixis.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.activitys.ReportDetailsActivity;
import com.lyp.jsonbean.LoginBean;
import com.lyp.jsonbean.MyReportBean;
import com.tjl.yangxixis.OriginalFragment;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.activity.MainActivity;
import com.tjl.yangxixis.api.YangxixiApi;
import com.tjl.yangxixis.utils.uitl;

import java.text.DecimalFormat;
import java.util.ArrayList;

import cn.qqtheme.framework.picker.OptionPicker;
import cn.qqtheme.framework.widget.WheelView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//销售员的线索报告
public class ReportFragment extends OriginalFragment implements OnClickListener{

    View v;
    private Button mYear,mMonth,mDay,mSelect;
    private ArrayList<MyReportBean.DataBean>  mList = new ArrayList<>();
    private LoginBean.DataBean bean;
    private TextView mReport;
    private TextView mOrder;
    private TextView mAffirm;
    private TextView mBack;
    private TextView mDetails;

    @Override
    protected void lazyLoad() {

    }

    public View createView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        v= inflater.inflate(R.layout.activity_reports, null,true);
        initview();
        return v;
    }

    private void initview() {
        bean = ((MainActivity) getActivity()).dataBean;
        mYear = (Button) v.findViewById(R.id.btn_my_year);
        mMonth = (Button) v.findViewById(R.id.btn_my_month);
        mDay = (Button) v.findViewById(R.id.btn_my_day);
        mSelect = (Button) v.findViewById(R.id.btn_my_select);
        mReport = (TextView) v.findViewById(R.id.tv_reports_number);
        mOrder = (TextView) v.findViewById(R.id.tv_order_number);
        mAffirm = (TextView) v.findViewById(R.id.tv_affirm_number);
        mBack = (TextView) v.findViewById(R.id.tv_chargeback_number);
        mDetails = (TextView) v.findViewById(R.id.tv_reports_details);
        mYear.setOnClickListener(this);
        mMonth.setOnClickListener(this);
        mDay.setOnClickListener(this);
        mSelect.setOnClickListener(this);
        mDetails.setOnClickListener(this);

        myreport(bean.getId(),1, "2017");
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.btn_my_year:
                //year();
                onYearPicker(v);
                break;
            case R.id.btn_my_month:
                //month();
                onMonthPicker(v);
                break;
            case R.id.btn_my_day:
                //day();
                onDayPicker(v);
                break;
            case R.id.btn_my_select:
                String year = mYear.getText().toString().trim();
                String month = mMonth.getText().toString();
                String day = mDay.getText().toString();
                String date = year;
                int type = 1;
                if (month.equals("整")&&!day.equals("整")){
                    type = 1;
                    date = year;
                }
                else if (!month.equals("整")&&day.equals("整")){
                    type = 2;
                    date += "-"+month;
                }
                else if (!month.equals("整")&&!day.equals("整")){
                    type = 3;
                    date += "-"+month+"-"+day;
                }

                myreport(bean.getId(), type, date);
                mDetails.setVisibility(View.VISIBLE);
                LogUtils.i("tag",bean.getId()+"_"+type+"__"+ date);
                break;
            case R.id.tv_reports_details:
                    Intent intent = new Intent(getActivity(), ReportDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("databean", mList);
                    intent.putExtras(bundle);
                    startActivity(intent);
                break;
        }
    }

//    public void year(){
//        final String[] provices = new String[]{"2017", "2018", "2019", "2020", "2021"};
//        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//
//        builder.setTitle("年份选择");//可以为空
//        builder.setSingleChoiceItems(provices, 0,
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface arg0, int arg1) {
//                                /*提醒测试作用*/
//                        mYear.setText(provices[arg1]);
//                        arg0.dismiss();
//                    }
//                });
//        builder.create().show();
//    }
//
//
//    public void month(){
//        final String[] provices = new String[]{"整","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
//        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//
//        builder.setTitle("月份选择");//可以为空
//        /*！注意这里的方法以及参数哟！"0"表示弹出对话框时，第一个是被选中的*/
//        builder.setSingleChoiceItems(provices, 0,
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface arg0, int arg1) {
//                        mMonth.setText(provices[arg1]);
//                        arg0.dismiss();
//                    }
//                });
//        builder.create().show();
//    }
//    public void day(){
//        if (mMonth.getText().toString().equals("整")) {
//            ToastUtils.showShortSafe("请先选择月份!");
//            return;
//        }
//        uitl uitl = new uitl();
//        int ri = Integer.parseInt(uitl.calDays(mYear.getText().toString().trim(),mMonth.getText().toString().trim()));
//        ArrayList<String> mList = new ArrayList<>();
//        final String[] str = new String[ri + 1];
//        mList.add("整");
//        str[0] = "整";
//        for (int i = 1; i < ri; i++) {
//            mList.add((i) + "");
//            str[i] = (i) + "";
//        }
//        final int size = mList.size();
//        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//
//        builder.setTitle("日选择");//可以为空
//        /*！注意这里的方法以及参数哟！"0"表示弹出对话框时，第一个是被选中的*/
//        builder.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
//
//            @Override
//            public void onClick(DialogInterface arg0, int arg1) {
//                                /*提醒测试作用*/
//                if (arg1 != 0) {
//                    mDay.setText(new DecimalFormat("00").format(Integer.parseInt(str[arg1])));
//                } else {
//                    mDay.setText(str[arg1]);
//                }
//                arg0.dismiss();
//            }
//        });
//        builder.create().show();
//    }

    public void onYearPicker(View view) {
        final String[] provices = new String[]{"2017", "2018", "2019", "2020", "2021","2022","2023","2024","2025"};
        OptionPicker picker = new OptionPicker(getActivity(), provices);
        picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);
        picker.setShadowColor(Color.WHITE, 40);
        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setTitleText("年选择");
        picker.setTitleTextSize(ConvertUtils.sp2px(10));
        picker.setTextSize(ConvertUtils.sp2px(10));
        picker.setPadding(ConvertUtils.sp2px(10));
        picker.setCancelTextSize(ConvertUtils.sp2px(10));
        picker.setSubmitTextSize(ConvertUtils.sp2px(10));
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                mYear.setText(provices[index]);
            }
        });
        picker.show();
    }

    public void onMonthPicker(View view) {
        final String[] provices = new String[]{"整","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        OptionPicker picker = new OptionPicker(getActivity(), provices);
        picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);
        picker.setShadowColor(Color.WHITE, 40);
        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setTitleText("月选择");
        picker.setTitleTextSize(ConvertUtils.sp2px(10));
        picker.setTextSize(ConvertUtils.sp2px(10));
        picker.setPadding(ConvertUtils.sp2px(10));
        picker.setCancelTextSize(ConvertUtils.sp2px(10));
        picker.setSubmitTextSize(ConvertUtils.sp2px(10));
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                mMonth.setText(provices[index]);
            }
        });
        picker.show();
    }

    public void onDayPicker(View view) {
        if (mMonth.getText().toString().equals("整")) {
            ToastUtils.showShortSafe("请先选择月份!");
            return;
        }
        uitl uitl = new uitl();
        int ri = Integer.parseInt(uitl.calDays(mYear.getText().toString().trim(),mMonth.getText().toString().trim()));
        ArrayList<String> mList = new ArrayList<>();
        final String[] str = new String[ri + 1];
        mList.add("整");
        str[0] = "整";
        for (int i = 1; i < ri; i++) {
            mList.add((i) + "");
            str[i] = (i) + "";
        }
        OptionPicker picker = new OptionPicker(getActivity(), mList);
        picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);
        picker.setShadowColor(Color.WHITE, 40);
        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setTitleText("日选择");
        picker.setTitleTextSize(ConvertUtils.sp2px(10));
        picker.setTextSize(ConvertUtils.sp2px(10));
        picker.setPadding(ConvertUtils.sp2px(10));
        picker.setCancelTextSize(ConvertUtils.sp2px(10));
        picker.setSubmitTextSize(ConvertUtils.sp2px(10));
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                if (index != 0) {
                    mDay.setText(new DecimalFormat("00").format(Integer.parseInt(str[index])));
                } else {
                    mDay.setText(str[index]);
                }
            }
        });
        picker.show();
    }

    //销售员报告
    public void myreport(String a_id,int type,String datetime) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<MyReportBean> call = api.getMyReport(a_id,type,datetime);
        call.enqueue(new Callback<MyReportBean>() {
            @Override
            public void onResponse(Call<MyReportBean> call, final Response<MyReportBean> response) {
                if (response.body().getResult() == 1 ) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (response.body().getData().getO_ids() == "") {
                                mDetails.setVisibility(View.GONE);
                            }
                            else {
                                mDetails.setVisibility(View.VISIBLE);
                            }
                            mList.clear();
                            mList.add(response.body().getData());
                            mReport.setText(mList.get(0).getOrder_nums()+"");
                            mOrder.setText(mList.get(0).getSingle_nums()+"");
                            mAffirm.setText(mList.get(0).getAffirm_nums()+"");
                            mBack.setText(mList.get(0).getRefund_nums()+"");
                        }
                    });
                } else{
                    ToastUtils.showShort(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<MyReportBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }
}
